package encaps.action;

import encaps.entities.Grant;
import encaps.entities.Role;
import encaps.entities.UserLocal;
import encaps.entities.UserLocalHome;
import encaps.resources.Config;
import encaps.resources.Labels;
import encaps.security.RoleManager;
import encaps.utils.*;
import java.util.HashMap;
import java.util.Map;
import org.jdom.Element;

public class ApplyUserChangesAction extends EditUserAction {

    public static final String APPLY_USER_CHANGES = "applyUserChanges";

    public ApplyUserChangesAction(ActionContext context, Grant requiredGrant) {
        super(context, requiredGrant);
    }

    @Override
    public Element execute() throws ActionExecutionException {
        try {
            //System.out.println("Act con: "+actionContext);
            //System.out.println("Act con id: "+actionContext.getTextParameter(ID));
            Long id = Long.parseLong(actionContext.getTextParameter(ID));
            UserLocalHome userHome = EntityUtil.getUserHome();
            UserLocal user = userHome.findByPrimaryKey(id);
            Map<String, String> parameters = validatePassword(user);

            if (parameters != null) {
                Role role = RoleManager.getRoleManager().getRole(actionContext.getUserRole());
                return createUserForm(null, role, parameters);
            } else {
                String password = actionContext.getTextParameter(PASSWORD);
                if (!StringUtil.isEmpty(password)) {
                    user.setPassword(actionContext.getTextParameter(PASSWORD));
                }
                user.setInfo(actionContext.getTextParameter(INFO));
                user.setEmail(actionContext.getTextParameter(EMAIL));
                user.setContacts(actionContext.getTextParameter(CONTACTS));
                if (Provider.isExist(RoleGrantGenreEnum.ROLE, actionContext.getTextParameter(ROLE))) {
                    user.setRole(actionContext.getTextParameter(ROLE));
                }

                String picName = String.format("%d-%s", user.getID(), user.getLogin());
                String picPath = IOHelper.uploadFile(actionContext, PIC, picName, Config.getUserImagesDirectory());
                if (picPath != null) {
                    user.setAvatar(picPath);
                }

                return XMLHelper.createTextMessage(Labels.getLabel(Labels.USER_CHANGED_MESSAGE));
            }
        } catch (Exception ex) {
            throw new ActionExecutionException(Labels.getLabel(Labels.CANNOT_EXECUTE_ACTION), ex);
        }
    }

    private Map<String, String> validatePassword(UserLocal user) throws SystemException {

        String password = actionContext.getTextParameter(PASSWORD);
        String rePassword = actionContext.getTextParameter(PASSWORD_REPEAT);
        if (StringUtil.isEmpty(password) && StringUtil.isEmpty(rePassword)) {
            return null;
        }
        String passMessage = null;
        String rePassMessage = null;
        if (StringUtil.isEmpty(password)) {
            passMessage = Labels.getLabel(Labels.ENTER_PASSWORD_MESSAGE);
        }
        if (!password.equals(rePassword)) {
            rePassMessage = Labels.getLabel(Labels.PASSWORDS_MISMATCH_MESSAGE);
        }
        if (passMessage == null && rePassMessage == null) {
            return null;
        }
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put(ID, user.getID().toString());
        parameters.put(PASSWORD_MESSAGE, passMessage);
        parameters.put(PASSWORD_REPEAT_MESSAGE, rePassMessage);
        parameters.put(LOGIN, user.getLogin());
        parameters.put(INFO, actionContext.getTextParameter(INFO));
        parameters.put(CONTACTS, actionContext.getTextParameter(CONTACTS));
        parameters.put(EMAIL, actionContext.getTextParameter(EMAIL));
        parameters.put(PIC, user.getAvatar());

        return parameters;
    }
}
